package com.example.vv.model.setupTreeList.base.presenter;

import com.example.vv.fragmentWork.entity.ArticleListEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.model.setupTreeList.base.contract.STLContract;
import com.example.vv.model.setupTreeList.base.model.STLModel;
import com.example.vv.res.net.BaseResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class STLPresenter extends MvpPresenter<STLContract.View,STLContract.Model> implements STLContract.Presenter {
    @Override
    public void attach(STLContract.View view) {
        super.attach(view);
        model = new STLModel();
    }

    @Override
    public void setArticleList(String api) {
        model.GET_ARTICLE_LIST(api)
                .map(new Function<BaseResponse, ArticleListEntity>() {
                    @Override
                    public ArticleListEntity apply(BaseResponse baseResponse) throws Exception {
                        return new Gson().fromJson(baseResponse.getData(),ArticleListEntity.class);
                    }
                }).subscribe(new Consumer<ArticleListEntity>() {
            @Override
            public void accept(ArticleListEntity listEntities) throws Exception {
                view().getArticleList(listEntities.getDatas());
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });
    }
}
